//
//  AudioBookCatagoryViewController.m
//  FunRead
//
//  Created by  King on 15/5/27.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "AudioBookCatagoryViewController.h"
#import "SANetWorkingTask.h"
#import "AudioBookCatagoryCell.h"
#import "AudioBookCatagoryModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "AudioBookDetailViewController.h"
@interface AudioBookCatagoryViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *collectionArray;
@end

@implementation AudioBookCatagoryViewController
- (void)dealloc
{
    [_name release];
    [_collectionArray release];
    [_collectionView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // 初始化集合数组（必须初始化）
        self.collectionArray = [NSMutableArray array];
        
    }
    return self;
}
#pragma mark - 加载视图功能
- (void)viewDidLoad {
    self.navigationItem.title = self.name;
    
    // 设置页面背景图片
    UIImageView *backImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    backImage.image = [UIImage imageNamed:@"back02.jpg"];
    [self.view addSubview:backImage];
    [backImage release];
    
    [self startConnection];
    [self createCollectionView];
}

#pragma mark - 网络请求
- (void)startConnection
{
    __block AudioBookCatagoryViewController *cagagory = self;
    NSString *str = [NSString stringWithFormat:@"http://app.9nali.com/961/common_tag/3/%@?page_id=1&device=iPad&version=1.1.2", self.name];
    [SANetWorkingTask request:str parmater:nil block:^(NSDictionary *result) {
        AudioBookCatagoryModel *model = [AudioBookCatagoryModel alloc];
        cagagory.collectionArray = [result objectForKey:@"list"];
        [self.collectionView reloadData];
        [model release];
    }];
}
#pragma mark - 创建类目集合视图
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置item边框距离和大小
    layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    layout.itemSize = CGSizeMake(self.view.frame.size.width  / 4 , (self.view.frame.size.height - 64 - 49) / 4);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64 - 49) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
    // 设置代理
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    // 注册重用池
    [_collectionView registerClass:[AudioBookCatagoryCell class] forCellWithReuseIdentifier:@"cell"];
    [self.view addSubview:_collectionView];
    [layout release];
    [_collectionView release];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectionArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    AudioBookCatagoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    AudioBookCatagoryModel *model = [[AudioBookCatagoryModel alloc] init];
    NSDictionary *dic = [self.collectionArray objectAtIndex:indexPath.row];
    [model setValuesForKeysWithDictionary:dic];
    [cell.coverSmall sd_setImageWithURL:[NSURL URLWithString:model.coverSmall] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.play_counts.text = [NSString stringWithFormat:@"▶️%@", model.plays_counts];
    cell.title.text = model.title;
    
    
    [model release];
    return cell;
}
// 点击item跳转到详情界面
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    AudioBookDetailViewController *detail = [[AudioBookDetailViewController alloc] init];
    NSDictionary *dic = [self.collectionArray objectAtIndex:indexPath.row];
    AudioBookCatagoryModel *model = [[AudioBookCatagoryModel alloc] init];
    [model setValuesForKeysWithDictionary:dic];
    detail.id = model.id;
    detail.name = model.title;
    [self.navigationController pushViewController:detail animated:YES];
    [model release];
    [detail release];
}
@end
